/* Copyright (c) 2011 William Steven Knauer
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.williamknauer.android.multitouchdetector.demo;

import com.williamknauer.android.multitouchdetector.MultitouchDetector;
import com.williamknauer.android.multitouchdetector.MultitouchListener;
import com.williamknauer.android.multitouchdetector.R;

import android.app.Activity;
import android.graphics.Matrix;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

/**
 * Demonstration of how to use a {@link MultitouchDetector} in an {@link Activity}. Uses multitouch
 * gestures to manipulate an image on the screen.
 * 
 * @author William S Knauer <knauerw1@gmail.com>
 * @version 2011.0908
 */
public class MultitouchDetectorDemoActivity extends Activity implements MultitouchListener {
	
	/** A tag for identifying which debug messages belong to this class */
	private static final String TAG = "MultitouchDetectorDemo";
	
	/** A handle to the view used to display an image */
	private ImageView mImageView;
	
	/** The matrix that will be used to transform the image */
	private Matrix mImageMatrix;
	
	/** The {@link MultitouchDetector} used to interpret gestures from touch events */
	private MultitouchDetector mMultitouchDetector;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		//initialize the matrix
		mImageMatrix = new Matrix();

		// get handle to imageview
		mImageView = (ImageView) findViewById(R.id.space_imageview);
		mImageView.setImageMatrix(mImageMatrix);
		
		// create MultitouchDetector with this class as its listener and wait for callbacks
		mMultitouchDetector = new MultitouchDetector(this);
	}

	/** Passes touch events to the {@link MultitouchDetector}. */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return mMultitouchDetector.onTouchEvent(event) || super.onTouchEvent(event);
	}
	
	private void log(String msg) {
		Log.d(TAG, msg);
	}

	@Override
	public boolean onMultiDrag(float x1, float y1, float x2, float y2, float focusX, float focusY,
			float dx, float dy) {
		StringBuilder sb = new StringBuilder();
		sb.append("onMultiDrag:");
		sb.append(" x1=");
		sb.append(x1);
		sb.append(" y1=");
		sb.append(y1);
		sb.append(" x2=");
		sb.append(x2);
		sb.append(" y2=");
		sb.append(y2);
		sb.append(" focusX=");
		sb.append(focusX);
		sb.append(" focusY=");
		sb.append(focusY);
		sb.append(" dx=");
		sb.append(dx);
		sb.append(" dy=");
		sb.append(dy);
		log(sb.toString());
		
		// drag image
		mImageMatrix.postTranslate(dx, dy);
		mImageView.setImageMatrix(mImageMatrix);
		
		return true;
	}

	@Override
	public boolean onMultiDragBegin(float x1, float y1, float x2, float y2, float focusX,
			float focusY) {
		StringBuilder sb = new StringBuilder();
		sb.append("onMultiDragBegin:");
		sb.append(" x1=");
		sb.append(x1);
		sb.append(" y1=");
		sb.append(y1);
		sb.append(" x2=");
		sb.append(x2);
		sb.append(" y2=");
		sb.append(y2);
		sb.append(" focusX=");
		sb.append(focusX);
		sb.append(" focusY=");
		sb.append(focusY);
		log(sb.toString());
		return true;
	}

	@Override
	public boolean onMultiDragEnd(float x1, float y1, float x2, float y2, float focusX,
			float focusY, float totalDistX, float totalDistY) {
		StringBuilder sb = new StringBuilder();
		sb.append("onMultiDragEnd:");
		sb.append(" x1=");
		sb.append(x1);
		sb.append(" y1=");
		sb.append(y1);
		sb.append(" x2=");
		sb.append(x2);
		sb.append(" y2=");
		sb.append(y2);
		sb.append(" focusX=");
		sb.append(focusX);
		sb.append(" focusY=");
		sb.append(focusY);
		sb.append(" totalDistX=");
		sb.append(totalDistX);
		sb.append(" totalDistY=");
		sb.append(totalDistY);
		log(sb.toString());
		return true;
	}

	@Override
	public boolean onScale(float x1, float y1, float x2, float y2, float focusX, float focusY,
			float scale) {
		StringBuilder sb = new StringBuilder();
		sb.append("onScale:");
		sb.append(" x1=");
		sb.append(x1);
		sb.append(" y1=");
		sb.append(y1);
		sb.append(" x2=");
		sb.append(x2);
		sb.append(" y2=");
		sb.append(y2);
		sb.append(" focusX=");
		sb.append(focusX);
		sb.append(" focusY=");
		sb.append(focusY);
		sb.append(" scale=");
		sb.append(scale);
		log(sb.toString());
		
		// scale image
		mImageMatrix.postScale(scale, scale, focusX, focusY);
		mImageView.setImageMatrix(mImageMatrix);
				
		return true;
	}

	@Override
	public boolean onScaleBegin(float x1, float y1, float x2, float y2, float focusX, float focusY) {
		StringBuilder sb = new StringBuilder();
		sb.append("onScaleBegin:");
		sb.append(" x1=");
		sb.append(x1);
		sb.append(" y1=");
		sb.append(y1);
		sb.append(" x2=");
		sb.append(x2);
		sb.append(" y2=");
		sb.append(y2);
		sb.append(" focusX=");
		sb.append(focusX);
		sb.append(" focusY=");
		sb.append(focusY);
		log(sb.toString());
		return true;
	}

	@Override
	public boolean onScaleEnd(float x1, float y1, float x2, float y2, float focusX, float focusY,
			float totalScale) {
		StringBuilder sb = new StringBuilder();
		sb.append("onScaleEnd:");
		sb.append(" x1=");
		sb.append(x1);
		sb.append(" y1=");
		sb.append(y1);
		sb.append(" x2=");
		sb.append(x2);
		sb.append(" y2=");
		sb.append(y2);
		sb.append(" focusX=");
		sb.append(focusX);
		sb.append(" focusY=");
		sb.append(focusY);
		sb.append(" totalScale=");
		sb.append(totalScale);
		log(sb.toString());
		return true;
	}

	@Override
	public boolean onSingleDrag(float x, float y, float dx, float dy) {
		StringBuilder sb = new StringBuilder();
		sb.append("onSingleDrag:");
		sb.append(" x=");
		sb.append(x);
		sb.append(" y=");
		sb.append(y);
		sb.append(" dx=");
		sb.append(dx);
		sb.append(" dy=");
		sb.append(dy);
		log(sb.toString());
		
		// drag image
		mImageMatrix.postTranslate(dx, dy);
		mImageView.setImageMatrix(mImageMatrix);
			
		return true;
	}

	@Override
	public boolean onSingleDragBegin(float x, float y) {
		StringBuilder sb = new StringBuilder();
		sb.append("onSingleDragBegin:");
		sb.append(" x=");
		sb.append(x);
		sb.append(" y=");
		sb.append(y);
		log(sb.toString());
		return true;
	}

	@Override
	public boolean onSingleDragEnd(float x, float y, float totalDistX, float totalDistY) {
		StringBuilder sb = new StringBuilder();
		sb.append("onSingleDragEnd:");
		sb.append(" x=");
		sb.append(x);
		sb.append(" y=");
		sb.append(y);
		sb.append(" totalDistX=");
		sb.append(totalDistX);
		sb.append(" totalDistY=");
		sb.append(totalDistY);
		log(sb.toString());
		return true;
	}

	@Override
	public boolean onSingleTap(float x, float y) {
		StringBuilder sb = new StringBuilder();
		sb.append("onSingleTap:");
		sb.append(" x=");
		sb.append(x);
		sb.append(" y=");
		sb.append(y);
		log(sb.toString());
		
		// rotate image
		mImageMatrix.postRotate(180.0f, x, y);
		mImageView.setImageMatrix(mImageMatrix);
		
		return true;
	}

}